using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;


namespace MSNRandomPersonalMessage
{
    public partial class Form1 : Form
    {

        [DllImport("user32", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(int Hwnd, int wMsg, int wParam, int lParam);

        [DllImport("user32", EntryPoint = "FindWindowExA")]
        private static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);


        public struct COPYDATASTRUCT
        {
            public int dwData;
            public int cbData;
            public int lpData;
        }

        public COPYDATASTRUCT data;


        public int VarPtr(object e)
        {
            GCHandle GC = GCHandle.Alloc(e, GCHandleType.Pinned);
            int gc = GC.AddrOfPinnedObject().ToInt32();
            GC.Free();
            return gc;
        }
        private const short WM_COPYDATA = 74;

        private void SendMSNMessage(bool enable, string category, string message)
        {
            string buffer = "\\0" + category + "\\0" + (enable ? "1" : "0") + "\\0{0}\\0" + message + "\\0\\0\\0\\0\0";
            int handle = 0;

            data.dwData = 0x0547;
            data.lpData = VarPtr(buffer);
            data.cbData = buffer.Length * 2;

            handle = FindWindowEx(0, handle, "MsnMsgrUIManager", null);
            if (handle > 0)
                SendMessage(handle, WM_COPYDATA, 0, VarPtr(data));
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            setQuote();
            
        }

        private void exitButton1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newButton1_Click(object sender, EventArgs e)
        {
            setQuote();
        }

        private void setQuote()
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(new System.IO.FileStream("jutalom.gwg", System.IO.FileMode.Open, System.IO.FileAccess.Read), Encoding.Default);
            
            String s;
            System.Collections.Generic.List<String> s1 = new List<String>();
            while ((s = reader.ReadLine()) != null)
            {
                s1.Add(s);
            }
            Random r = new Random();
            int i = r.Next(s1.Count);
            String prize = "(G)Mai jutalmad " + s1[i] + "(G)";
            SendMSNMessage(true, "Office", prize);
            textBox1.Text = prize;
            notifyIcon1.BalloonTipTitle = "MSN personal message changed!";
            notifyIcon1.BalloonTipText = prize;
            notifyIcon1.ShowBalloonTip(5000);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newPrizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setQuote();
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
        }
    }
}